package lxt.service.impl;

import java.util.List;

import lxt.dao.ClientDao;
import lxt.entity.ClientInfo;
import lxt.service.ClientService;
/**
 * 客户管理service
 * @author Lit
 */
public class ClientServiceImpl implements ClientService {
	
	private ClientDao  clientDao;
	/**
	 * 加载客户信息 
	 * @return
	 */
	public List<ClientInfo> loadClient(String linkman,String address,String clientName, int offset, int length )throws Exception {
		
		return (List<ClientInfo>) this.clientDao.loadAllClient(linkman,address,clientName,offset,length);
	}
	/**
	 * 添加客户
	 */
	public boolean  addClient(ClientInfo client)throws Exception {
		try{
			this.clientDao.save(client);
			return true;
		}catch(Exception ex){
			ex.printStackTrace();
			return false;
		}
		
	}
	/**
	 * 根据id查找客户
	 */
	public ClientInfo loadClientById(String clientId) throws Exception{
		System.out.println(clientId == null);
		return (ClientInfo) this.clientDao.get(ClientInfo.class, new String(clientId));
	}
	/**
	 * 修改客户
	 */
	public boolean updateClient(ClientInfo client)throws Exception{
		this.clientDao.update(client);
		return true;
	}
	/**
	 * 删除客户
	 */
	public boolean deleteClient(String clientId)throws Exception{
		String[] delId = clientId.split(",");
		for(int i=0;i<delId.length;i++){
			String id = delId[i];
			if(clientDao.loadCountOrdersForClient(id) > 0){
				throw new Exception("用户存在订单记录,不能删除.");
			}
			this.clientDao.delete(ClientInfo.class, id);
		}
		return true;
	}
	/**
	 * 查询数量
	 */
	public int loadAllClientCount(String linkman,String address,String clientName)throws Exception{
		return this.clientDao.loadAllClientCount(linkman, address, clientName);
	}
	
	public int loadCountOrdersForClient(String clientId) throws Exception {
		return this.clientDao.loadCountOrdersForClient(clientId);
	}
	
	/**
	 * get set methods
	 * @return
	 */

	public ClientDao getClientDao() {
		return clientDao;
	}

	public void setClientDao(ClientDao clientDao) {
		this.clientDao = clientDao;
	}
}
